package net.lcheng.service.impl;

import net.lcheng.dao.UserShippingAddressMapper;
import net.lcheng.model.UserShippingAddress;
import net.lcheng.model.UserShippingAddressExample;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.UserShippingAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by zwr on 2016/6/29 0029.
 */
@Service
public class UserShippingAddressServiceImpl implements UserShippingAddressService {
    @Resource
 private  UserShippingAddressMapper mapper;
    @Autowired
    private BaseAreasService baseAreasService;
    @Override
    public List<UserShippingAddress> getUserShippingAddressByUserUserId(String userId) {
        UserShippingAddressExample example=new UserShippingAddressExample();
        example.or().andUserUserIdEqualTo(userId);

        return mapper.selectByExample(example);
    }

    @Override
    public UserShippingAddress getAddressByUserId(String userId) {
        UserShippingAddressExample example=new UserShippingAddressExample();
        example.or().andConsigneePhoneEqualTo(userId);

        List<UserShippingAddress>list=mapper.selectByExample(example);
        if(list!=null&&list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public UserShippingAddress getAddressByAddressId(String addressId) {

        return mapper.selectByPrimaryKey(addressId);
    }

    @Override
    public int insert(String provinceName,String cityName,String districtName,String userId, String districtCode, String provinceCode, String cityCode, String consigneeName, String consigneePhone, String address, Integer isDefault,String zipCode) {
        UserShippingAddress add=new UserShippingAddress();
        add.setAddressId(UUID.randomUUID().toString());
        add.setConsigneeName(consigneeName);
        add.setConsigneePhone(consigneePhone);
        add.setAddress(address);
        if (isDefault==null)isDefault=0;
        add.setIsDefault(isDefault);
        add.setUserUserId(userId);
        add.setProvinceCode(provinceCode);
        add.setCityCode(cityCode);
        add.setDistrictCode(districtCode);
        add.setProvinceName(provinceName);
        add.setCityName(cityName);
        add.setDistrictName(districtName);
        add.setZipCode(zipCode);
        add.setCreateOn(new Date());
        return mapper.insert(add);
    }

    @Override
    public int edit(String addressId, String provinceName, String cityName, String districtName, String districtCode, String provinceCode, String cityCode, String consigneeName, String consigneePhone, String address, Integer isDefault,String zipCode) {
        UserShippingAddress add=new UserShippingAddress();
        add.setAddressId(addressId);
        add.setConsigneeName(consigneeName);
        add.setConsigneePhone(consigneePhone);
        add.setAddress(address);
        if(isDefault==null) isDefault=0;
        add.setIsDefault(isDefault);
        add.setProvinceCode(provinceCode);
        add.setCityCode(cityCode);
        add.setDistrictCode(districtCode);
        add.setProvinceName(provinceName);
        add.setCityName(cityName);
        add.setDistrictName(districtName);
        add.setZipCode(zipCode);
        return mapper.updateByPrimaryKeySelective(add);
    }

    @Override
    public int deleteAddress(String addressId) {

        return mapper.deleteByPrimaryKey(addressId);
    }

    @Override
    public UserShippingAddress getShippingAddressByUserIdAndDefaultType(String buyerUserId) {
        UserShippingAddressExample example=new UserShippingAddressExample();
        example.createCriteria().andUserUserIdEqualTo(buyerUserId).andIsDefaultEqualTo(1);
        List<UserShippingAddress> list=mapper.selectByExample(example);
        if (list!=null&&list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public UserShippingAddress getShippingAddressByPrimaryKey(String primaryKey) {
        return mapper.selectByPrimaryKey(primaryKey);
    }

    @Override
    public int insertShippingAddress(UserShippingAddress address) {
        return mapper.insertSelective(address);
    }

    @Override
    public int editShippingAddressByPrimaryKey(UserShippingAddress address) {
        return mapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public int updateIsDefault(String addressId, int isDefault) {
        UserShippingAddress userShippingAddress=new UserShippingAddress();
        userShippingAddress.setAddressId(addressId);
        userShippingAddress.setIsDefault(isDefault);
        return mapper.updateByPrimaryKeySelective(userShippingAddress);
    }

}
